package com.agu.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class CompareUtils {

	public static void main(String[] args) {

	}
	
	/**
	 * 获取对象的备注
	 * @param class1
	 * @param cmap
	 * @return
	 */
	public static String getObjectRemark(Object class1,Map<String,String> cmap) {
		Map<String,Object> markmap = new LinkedHashMap<String,Object>();
		
		try {
			Class<?> clazz1 = class1.getClass();
			Field[] field1 = clazz1.getDeclaredFields();
			
			// 遍历属性列表field1
			for (int i = 0; i < field1.length; i++) {
				field1[i].setAccessible(true);
				String name = field1[i].getName();
				if(cmap.containsKey(name))
				{
					String key = "【"+cmap.get(name)+"】";
					markmap.put(key,field1[i].get(class1));
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String json = JsonUtils.toJson(markmap);
		return FormatUtil.formatJson(json);
	}
	
	public static Map getObjectMap(Object class1,Map<String,String> cmap) {
		Map<String,Object> markmap = new LinkedHashMap<String,Object>();
		
		try {
			Class<?> clazz1 = class1.getClass();
			Field[] field1 = clazz1.getDeclaredFields();
			
			// 遍历属性列表field1
			for (int i = 0; i < field1.length; i++) {
				field1[i].setAccessible(true);
				String name = field1[i].getName();
				if(cmap.containsKey(name))
				{
					String key = "【"+cmap.get(name)+"】";
					markmap.put(key,field1[i].get(class1));
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return markmap;
	}
	
	
	public static String getTwoObjectRemark(Object class1, Object class2, Map<String,String> cmap) {
		Map<String,Object> markmap = new LinkedHashMap<String,Object>();
		
		try {
			Class<?> clazz1 = class1.getClass();
			Class<?> clazz2 = class2.getClass();
			// 获取对象的属性列表
			Field[] field1 = clazz1.getDeclaredFields();
			Field[] field2 = clazz2.getDeclaredFields();
			
			// 遍历属性列表field1
			for (int i = 0; i < field1.length; i++) {
				field1[i].setAccessible(true);
				field2[i].setAccessible(true);
				
				String name = field1[i].getName();
				String value1 = String.valueOf(field1[i].get(class1));
				String value2 = String.valueOf(field2[i].get(class2));
				String value = "";
				
				boolean b = compareTwo(value1,value2);
				
				
				System.out.println(name);
				if(cmap.containsKey(name))
				{
					String key = "【"+cmap.get(name)+"】";
					if(b)
					{
						markmap.put(key,value1);
					}else {
						Map map = new HashMap();
						map.put("修改前", value1);
						map.put("修改后", value2);
						markmap.put(key,map);
					}
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String json = JsonUtils.toJson(markmap);
		return FormatUtil.formatJson(json);
	}
	
	public static Map getTwoObjectMap(Object class1, Object class2, Map<String,String> cmap) {
		Map<String,Object> markmap = new LinkedHashMap<String,Object>();
		
		try {
			Class<?> clazz1 = class1.getClass();
			Class<?> clazz2 = class2.getClass();
			// 获取对象的属性列表
			Field[] field1 = clazz1.getDeclaredFields();
			Field[] field2 = clazz2.getDeclaredFields();
			
			// 遍历属性列表field1
			for (int i = 0; i < field1.length; i++) {
				field1[i].setAccessible(true);
				field2[i].setAccessible(true);
				
				String name = field1[i].getName();
				String value1 = String.valueOf(field1[i].get(class1));
				String value2 = String.valueOf(field2[i].get(class2));
				String value = "";
				
				boolean b = compareTwo(value1,value2);
				
				
				System.out.println(name);
				if(cmap.containsKey(name))
				{
					String key = "【"+cmap.get(name)+"】";
					if(b)
					{
						markmap.put(key,value1);
					}else {
						Map map = new HashMap();
						map.put("修改前", value1);
						map.put("修改后", value2);
						markmap.put(key,map);
					}
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return markmap;
	}
	
	public static String compareTwoClass(Object class1, Object class2,Set<String> ignoreset,Map<String,String> cmap) {
		StringBuilder sb = new StringBuilder();
		// 获取对象的class
		try {
			Class<?> clazz1 = class1.getClass();
			Class<?> clazz2 = class2.getClass();
			// 获取对象的属性列表
			Field[] field1 = clazz1.getDeclaredFields();
			Field[] field2 = clazz2.getDeclaredFields();
			// 遍历属性列表field1
			for (int i = 0; i < field1.length; i++) {

				// 如果field1[i]属性名与field2[j]属性名内容相同
				if (!field1[i].getName().equals(field2[i].getName())) {
					continue;
				}
				field1[i].setAccessible(true);
				field2[i].setAccessible(true);

				Object obj1 = field1[i].get(class1);
				Object obj2 = field2[i].get(class2);
				//System.out.println("========================");
				//System.out.println(obj1);
				//System.out.println(obj2);
				boolean b1 = isBaseType(obj1);
				boolean b2 = isBaseType(obj2);
				if (b1 && b2) {
					if(!ignoreset.contains(field1[i].getName()))
					{
						// 如果field1[i]属性值与field2[j]属性值内容不相同
						if (!compareTwo(field1[i].get(class1), field2[i].get(class2))) {
							
							if(cmap.containsKey(field1[i].getName()))
							{
								String colname = field1[i].getName();
								sb.append(cmap.get(colname));
								sb.append("["+field1[i].getName()+"]");
								sb.append("【" + field1[i].get(class1));
								sb.append("->" + field2[i].get(class2));
								sb.append("】，\n");
							}else {
								sb.append(field1[i].getName());
								sb.append("【" + field1[i].get(class1));
								sb.append("->" + field2[i].get(class2));
								sb.append("】，\n");
							}
							

						}
					}

				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (sb.length() > 0) {
			return sb.deleteCharAt(sb.length() - 1).toString();
		} else {
			return sb.toString();
		}

	}

	public static boolean compareTwo(Object object1, Object object2) {
		if (object1 == null && object2 == null) {
			return true;
		}
		if (object1 == null && object2 != null) {
			return false;
		}
		if (object1.equals(object2)) {
			return true;
		}
		return false;
	}

	public static boolean isBaseType(Object object) {
		if (object == null) {
			return true;
		}

		Class className = object.getClass();
		if (className.equals(java.lang.Integer.class) || className.equals(java.lang.Byte.class)
				|| className.equals(java.lang.Long.class) || className.equals(java.lang.Double.class)
				|| className.equals(java.lang.Float.class) || className.equals(java.lang.Character.class)
				|| className.equals(java.lang.Short.class) || className.equals(java.lang.Boolean.class)) {
			return true;
		}
		return false;
	}
}